package com.foreknow.demo13;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class IOStudent extends Student {

    public void save(List<Student> list) {
        Student student = new Student();
        List<Student> arrayList = new ArrayList<>();
        arrayList.add(student);
        try {
            FileWriter fw = new FileWriter("Student.txt");
            BufferedWriter bw = new BufferedWriter(fw);
            Iterator<Student> it = list.iterator();
            while (it.hasNext()){
                Student stu = it.next();
                String str = stu.getId()+" "+stu.getName()+" "+
                        stu.getAge()+" "+stu.getSchool();
                bw.write(str);
                bw.newLine();
            }
            bw.close();
        } catch (IOException e) {
            System.out.println("保存失败");
            e.printStackTrace();
        }finally {
            System.out.println("保存成功");
        }
    }

    public void read(){
        try {
            FileReader fr = new FileReader("Student.txt");
            BufferedReader br = new BufferedReader(fr);
            //读取源文件中的一行
            String line = br.readLine();
            for (int i = 0;i<3;i++) {
                line = br.readLine();
                System.out.println(line);
            }
            //释放资源
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
